(async function () {
  async function getAttributeChangedNote() {
      return api.getNoteWithLabel("todoTaskAttributeChanged");
  }
  async function getBranchCreatedNote() {
      return api.getNoteWithLabel("todoBranchCreated");
  }     

  const taskNote = api.originEntity;
  const taskParentNote = taskNote.getParentNotes()[0];
  const taskAttributeChangedNote = await getAttributeChangedNote();
  const taskBranchCreated = await getBranchCreatedNote();    
      
  taskNote.addRelation("location", taskParentNote.noteId);  
  taskNote.setLabel("todoItem");
  taskNote.addRelation("runOnAttributeChange", taskAttributeChangedNote.noteId);
  taskNote.addRelation("runOnBranchCreation", taskBranchCreated.noteId);
  
  if (taskParentNote.hasLabel("todoDone")) {
    const today = api.dayjs().format("YYYY-MM-DD");

    taskNote.setLabel("completedDate", today);

    return;
  }
  if (taskParentNote.hasLabel("todoInProgress")) {
    await taskStartTracking(taskNote.noteId);

    return;
  }
})();
